package com.youlai.system.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.constant.SystemConstants;
import com.youlai.system.common.model.Option;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.form.MoldForm;
import com.youlai.system.model.query.MoldPageQuery;
import com.youlai.system.model.vo.MoldPageVO;
import com.youlai.system.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SysDictService;
import com.youlai.system.service.SysMoldService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 故障类型控制器
 */
@Tag(name = "21.故障类型接口")
@RestController
@RequestMapping("/api/v1/molds")
@RequiredArgsConstructor
public class SysMoldController {

    private final SysMoldService moldService;

    private final SysDictService dictService;

    @Operation(summary = "故障类型分页列表")
    @GetMapping("/page")
    public PageResult<MoldPageVO> getMoldPage(
            @ParameterObject MoldPageQuery queryParams
    ) {
        IPage<MoldPageVO> result = moldService.getMoldPage(queryParams);
        result.getRecords().forEach(moldPageVO -> {
            moldPageVO.setFrequencyLabel(dictService.getLabel(SystemConstants.FREQUENCY, moldPageVO.getFrequency()));
        });
        return PageResult.success(result);
    }

    @Operation(summary = "新增故障类型")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:mold:add')")
    @PreventDuplicateSubmit
    public Result saveMold(
            @RequestBody @Valid MoldForm moldForm
    ) {
        boolean result = moldService.saveMold(moldForm);
        return Result.judge(result);
    }

    @Operation(summary = "故障类型表单数据")
    @GetMapping("/{moldId}/form")
    public Result<MoldForm> getMoldForm(
            @Parameter(description = "故障类型ID") @PathVariable Long moldId
    ) {
        MoldForm formData = moldService.getMoldForm(moldId);
        return Result.success(formData);
    }


    @Operation(summary = "修改故障类型")
    @PutMapping(value = "/{moldId}")
    @PreAuthorize("@ss.hasPerm('sys:mold:edit')")
    public Result updateMold(
            @Parameter(description = "故障类型ID") @PathVariable Long moldId,
            @RequestBody @Validated MoldForm moldForm) {
        boolean result = moldService.updateMold(moldId, moldForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除故障类型")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:mold:delete')")
    public Result deleteMolds(
            @Parameter(description = "故障类型ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "故障类型删除数据为空");
        List<Long> idList = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        boolean result = moldService.deleteMolds(idList);
        return Result.judge(result);
    }

    @Operation(summary = "故障类型下拉列表")
    @GetMapping("/options")
    public Result<List<Option>> listMoldOptions() {
        List<Option> list = moldService.listMoldOptions();
        return Result.success(list);
    }

}
